<template>
	<view>
	</view>
</template>

<script lang="uts">
	import SVGACallback from 'com.opensource.svgaplayer.SVGACallback';
	import SVGAImageView from 'com.opensource.svgaplayer.SVGAImageView';
	import FillMode from 'com.opensource.svgaplayer.SVGAImageView.FillMode';
	import SVGAParser from 'com.opensource.svgaplayer.SVGAParser';
	import ParseCompletion from 'com.opensource.svgaplayer.SVGAParser.ParseCompletion';

	import SVGAVideoEntity from 'com.opensource.svgaplayer.SVGAVideoEntity';
	import TextUtils from 'android.text.TextUtils';
	import URL from 'java.net.URL';
	import FileInputStream from 'java.io.FileInputStream';
	import View from 'android.view.View';

	class ISVGACallback implements SVGACallback {

		comp : UTSComponent<SVGAImageView>
		constructor(comp : UTSComponent<SVGAImageView>) {
			super();
			this.comp = comp
		}

		override onFinished() : void {
			UTSAndroid.getDispatcher("main").async(function (_) {
				this.comp.$emit("onFinished", {});
			});
		}

		override onPause() : void {
			console.log("onPause")
		}

		override onRepeat() : void {
			console.log("onRepeat")
		}

		override onStep(frame : Int, percentage : Double) : void {
			UTSAndroid.getDispatcher("main").async(function (_) {
				this.comp.$emit("frame", { "frame": frame })
				this.comp.$emit("percentage", { "percentage": percentage })
			});
		}
	}

	class IParseCompletion implements ParseCompletion {
		comp : UTSComponent<SVGAImageView>
		constructor(comp : UTSComponent<SVGAImageView>) {
			super();
			this.comp = comp
		}

		override onComplete(videoItem : SVGAVideoEntity) : void {
			let imageView = this.comp.$el!
			imageView.setVideoItem(videoItem);
			imageView.startAnimation();
			UTSAndroid.getDispatcher("main").async(function (_) {
				this.comp.$emit("start", {});
			});
		}

		override onError() : void {
			UTSAndroid.getDispatcher("main").async(function (_) {
				this.comp.$emit("error", {});
			});
		}
	}

	export default {
		name: "svga-player",
		emits: ['start', 'error', 'finished', 'frame', 'percentage'],
		props: {
			/**
			* 动画资源地址，支持绝对路径和http、https地址
			*/
			"path": {
				type: String,
				default: ""
			},
			/**
			 * 动画是否循环播放
			 */
			"loop": {
				type: Boolean,
				default: false
			},
			/**
			 * Backward  Forward  Clear
			 */
			"fillMode": {
				type: String,
				default: "Backward"
			},
			/**
			 * 是否隐藏动画
			 */
			"hidden": {
				type: Boolean,
				default: false
			}
		},
		/**
		 * 组件内部变量声明
		 */
		data() {
			return {}
		},
		/**
		 * 属性变化监听器实现
		 */
		watch: {
			"path": {
				handler(newPath : string) {
					if (this.$el != null) {
						if (!TextUtils.isEmpty(newPath)) {
							try {
								let parser = new SVGAParser($androidContext!);
								if (newPath.startsWith("http://") || newPath.startsWith("https://")) {
									parser.decodeFromURL(new URL(newPath), new IParseCompletion(this), null);
								} else {
									var realJsonPath = UTSAndroid.getResourcePath(newPath)
									parser.decodeFromInputStream(new FileInputStream(realJsonPath), newPath, new IParseCompletion(this), true, null, "");
								}
							} catch (e) {
							}
						}
					}
				},
				immediate: false
			},
			"loop": {
				handler(newLoop : Boolean) {
					if (this.$el != null) {
						if (newLoop) {
							this.$el!.loops = 0
						} else {
							// 不循环则设置成1次
							this.$el!.loops = 1
						}
					}
				},
				immediate: false
			},
			"fillMode": {
				handler(newAction : string) {
					if (newAction == "Backward" || newAction == "Forward" || newAction == "Clear") {
						if (this.$el != null) {
							if (this.fillMode == "Backward") {
								this.$el!.fillMode = FillMode.Backward;
							} else if (this.fillMode == "Forward") {
								this.$el!.fillMode = FillMode.Forward;
							} else if (this.fillMode == "Clear") {
								this.$el!.fillMode = FillMode.Clear;
							}
						}
					} else {
						// 非法入参，不管
					}
				},
				immediate: false
			},
			"hidden": {
				handler(newValue : boolean) {
					if (this.$el != null) {
						if (newValue) {
							this.$el!.visibility = View.GONE
						} else {
							this.$el!.visibility = View.VISIBLE
						}
					}
				},
				immediate: false
			},
		},
		expose: ['loadUrl', 'startAnimation', 'pauseAnimation'],
		methods: {
			loadUrl(url : string) {
				if (!TextUtils.isEmpty(url)) {
					try {
						let parser = new SVGAParser($androidContext!);
						if (url.startsWith("http://") || url.startsWith("https://")) {
							parser.decodeFromURL(new URL(url), new IParseCompletion(this), null);
						} else {
							var realJsonPath = UTSAndroid.getResourcePath(url)
							parser.decodeFromInputStream(new FileInputStream(realJsonPath), url, new IParseCompletion(this), true, null, "");
						}
					} catch (e) {
					}
				}
			},
			startAnimation() {
				this.$el!.startAnimation();
			},
			pauseAnimation() {
				this.$el!.pauseAnimation();
			},
		},
		NVLoad() : SVGAImageView {
			let imageView = new SVGAImageView($androidContext!);
			imageView.callback = new ISVGACallback(this);
			return imageView;
		},
		NVLoaded() {
		}
	}
</script>

<style>

</style>